// loading 画面
package loading

import (
	"image/color"
	"log"

	"gitee.com/yysama/Kindling_civilization/core/assetloader"
	"gitee.com/yysama/Kindling_civilization/core/sprites"
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/text"
	"github.com/pkg/errors"
	"golang.org/x/image/font"
)

var (
	thin_font font.Face
)

func init() {
	_font, e := assetloader.NewFont("../asset/font/w4.ttf", 18, 72)
	if e != nil {
		log.Fatal(errors.Wrap(e, "fontface.New"))
	}
	thin_font = _font
}

type scence_loading struct {
	width           int
	height          int
	node            *sprites.Sprite // 背景板
	bar_left        *sprites.Sprite // 进度左边
	bar_right       *sprites.Sprite // 进度右边
	bar_bg_1        *sprites.Sprite // 已经完成
	bar_bg_2        *sprites.Sprite // 未完成
	bar_light       *sprites.Sprite // 进度灯
	percent         float64         // 进度条进度
	process_bar_len float64         // 进度条满长度
	tips            string          // tips 消息
	tips_color      color.RGBA
	lastScence      ebiten.Game
}

func New() *scence_loading {
	GeoM := ebiten.GeoM{}

	wx, wy := ebiten.WindowSize()
	node := sprites.NewByWH(wx, wy, &ebiten.DrawImageOptions{GeoM: GeoM})

	bg_Img := assetloader.NewImageFromFile("../asset/images/loading/bg.png")
	w, h := bg_Img.Size()
	GeoM.Reset()
	GeoM.Translate(float64((wx-w)/2), float64((wy-h)/2))
	//GeoM.Scale(float64(wx/w), float64(wy/h))
	bg := sprites.NewByImg(bg_Img, &ebiten.DrawImageOptions{GeoM: GeoM})
	node.AddChild(bg)

	// 960*65
	// bar_left_img := assetloader.NewImageFromFile("../asset/images/loading/bar_left.png")
	// gem.Reset()
	// gem.Translate(float64((wx-960)/2), float64(wy*4/5))
	// bar_left := sprites.NewByImg(bar_left_img, &ebiten.DrawImageOptions{GeoM: gem})
	// node.AddChild(bar_left)

	// bar_right_img := assetloader.NewImageFromFile("../asset/images/loading/bar_right.png")
	// gem.Reset()
	// gem.Translate(float64((wx+960)/2), float64(wy*4/5))
	// bar_right := sprites.NewByImg(bar_right_img, &ebiten.DrawImageOptions{GeoM: gem})
	// bg.AddChild(bar_right)

	// bar_bg_1_img := assetloader.NewImageFromFile("../asset/images/loading/bar_bg_1.png")
	// gem.Reset()
	// gem.Translate(float64((wx-960)/2)+20, float64(wy*4/5)+18)
	// bar_bg_1 := sprites.NewByImg(bar_bg_1_img, &ebiten.DrawImageOptions{GeoM: gem})
	// bg.AddChild(bar_bg_1)

	// bar_bg_2_img := assetloader.NewImageFromFile("../asset/images/loading/bar_bg_2.png")
	// gem.Reset()
	// gem.Translate(float64((wx-960)/2)+20, float64(wy*4/5)+11)
	// bar_bg_2 := sprites.NewByImg(bar_bg_2_img, &ebiten.DrawImageOptions{GeoM: gem})
	// bg.AddChild(bar_bg_2)

	// bar_light_img := assetloader.NewImageFromFile("../asset/images/loading/light.png")
	// gem.Reset()
	// gem.Translate(float64((wx-960)/2), float64(wy*4/5)-65)
	// bar_light := sprites.NewByImg(bar_light_img, &ebiten.DrawImageOptions{GeoM: gem})
	// bg.AddChild(bar_light)

	return &scence_loading{
		width:  wx,
		height: wy,
		node:   node,
		// bar_left:        bar_left,
		// bar_right:       bar_right,
		// bar_bg_1:        bar_bg_1,
		// bar_bg_2:        bar_bg_2,
		// bar_light:       bar_light,
		process_bar_len: 920,
	}
}

func (this *scence_loading) Draw(screen *ebiten.Image) {
	// 画一个背景图
	this.node.Draw(screen)

	// 画一个Tips
	if len(this.tips) > 0 {
		rect := text.BoundString(thin_font, this.tips)
		text.Draw(screen, this.tips, thin_font, (this.width-rect.Dx())/2, this.height*5/6+60, this.tips_color)
	}
}

func (this *scence_loading) Update() error {
	if this.percent >= 100 {
		if nil != this.lastScence {
			//game.Instance(nil).SetScence(this.lastScence)
		}

		return nil
	}

	return nil
}

func (this *scence_loading) Layout(outsideWidth, outsideHeight int) (int, int) {
	return this.width, this.height
}

// 进度条状态
func (this *scence_loading) Bar(percent float64) *scence_loading {
	this.percent = percent

	return this
}

// Tips
func (this *scence_loading) Tips(tips string, clr color.RGBA) *scence_loading {
	this.tips = tips
	this.tips_color = clr

	return this
}

// 跳到哪个场景
func (this *scence_loading) LastScence(lastScence ebiten.Game) *scence_loading {
	this.lastScence = lastScence

	return this
}
